package cn.hxzy.controller;


import cn.hxzy.domain.Section;
import cn.hxzy.domain.vo.SectionVo;
import cn.hxzy.response.R;
import cn.hxzy.response.StatusEnum;
import cn.hxzy.service.SectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author niuyi
 * @since 2024-06-22
 */
@RestController
@RequestMapping("/section")
public class SectionController {
    @Autowired
    private SectionService sectionService;

    /**
     * 根据章节编号获取小节列表
     *
     * @param chapterId
     * @return
     */
    @GetMapping("/selectList")
    public R<List<Section>> sectionList(@RequestParam Integer chapterId) {
        List<Section> sections = sectionService.sectionList(chapterId);
        if (!sections.isEmpty()) {
            return R.success(sections);
        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }

    /**
     * 修改小节
     *
     * @param sectionId
     * @return
     */
    @PutMapping("/updateSectionName")
    public R updateSectionName(@RequestParam("sectionId") Integer sectionId, @RequestParam("sectionName") String sectionName) {
        String s = sectionService.updateSectionName(sectionId, sectionName);
        if (s.length() > 0) {
            return R.success("修改成功");
        }
        return R.error(StatusEnum.PARAM_INVALID);
    }

    /**
     * 增加小节
     *
     * @param section
     * @return
     */
    @PostMapping("/increaseSection")
    public R increaseSection(@RequestBody Section section) {
        int i = sectionService.increaseSection(section);
        if (i > 0) {
            return R.success("新增成功");
        }
        return R.error(StatusEnum.PARAM_INVALID);
    }

    /**
     * 删除小节
     *
     * @param sectionId
     * @return
     */
    @DeleteMapping("/delete")
    public R deleteById(@RequestParam("sectionId") Integer sectionId) {
        int i = sectionService.deleteById(sectionId);
        if (i > 0) {
            return R.success("删除成功");
        }
        return R.error(StatusEnum.PARAM_INVALID);
    }

    /**
     * 模糊查询小节
     */
    @GetMapping("/getSectionByName")
    public R<List<Section>> selectSection(@RequestParam("sectionName") String sectionName) {
        List<Section> sections = sectionService.getSectionByName(sectionName);
        if (!sections.isEmpty()) {
            return R.success(sections);
        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }

    @GetMapping("/getList")
    public R<List<SectionVo>> getList(@RequestParam Integer courseId) {
        List<SectionVo> list = sectionService.getList(courseId);
        if (!list.isEmpty()) {
            return R.success(list);
        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }
}

